package cn.jietuo.first.class01;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/27
 * @description: 在一个有序数组中，找<=某个数最右侧的位置
 */
public class Code06_BSNearRight {


    public static int getNearRight(int[] arr, int i) {
        int l = 0;
        int r = arr.length - 1;
        int mid = 0;
        int index = -1;
        while (l <= r) {
            mid = ((r - l) >> 1) + l;
            if (arr[mid] <= i) {
                index = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return index;
    }

    public static void main(String[] args) {
        int[] array = new int[]{1, 3, 6, 8, 9, 29, 122, 234};
        int nearLeft = getNearRight(array, 10);
        System.out.println(nearLeft);
    }
}
